//
//  DrawerViewController.m
//  Moment
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 谭泽源. All rights reserved.
//

#import "DrawerViewController.h"
#import "DrawerTableViewCell.h"
#import "HeaderView.h"
#import "FooterView.h"


@interface DrawerViewController ()<UITableViewDataSource,UITableViewDelegate,SearchDelegate>


@property (nonatomic, strong) UITableView *drawTableView;
@property (nonatomic, strong) FooterView *footerView;
@property (nonatomic, strong) HeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *labelArray;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) NSMutableArray *newsPicArray;
@property (nonatomic, strong) NSIndexPath *tempIndex;



@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    
    [self createTableView];
    
}

- (void)createTableView
{
    self.drawTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    //self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    self.drawTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.drawTableView.delegate = self;
    self.drawTableView.dataSource = self;
    self.drawTableView.showsVerticalScrollIndicator = NO;
    self.drawTableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];;
    [self.drawTableView registerClass:[DrawerTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.drawTableView];
    
    
    UIView *nilHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4 + 10)];
    //nilHeaderView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    //nilHeaderView.backgroundColor = [UIColor whiteColor];
    self.drawTableView.tableHeaderView = nilHeaderView;
    
    UIView *nilFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 8)];
    //nilFooterView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    //nilFooterView.backgroundColor = [UIColor whiteColor];
    self.drawTableView.tableFooterView = nilFooterView;
    
//    
    self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4)];
#warning !!!!
    self.headerView.SearchDelegate =self;
    [self.view addSubview:self.headerView];
//
    self.footerView = [[FooterView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT * (7.0 / 8), SCREEN_WIDTH, SCREEN_HEIGHT / 8)];
    [self.view addSubview:self.footerView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    _footerView.userInteractionEnabled = YES;
    [_footerView addGestureRecognizer:tap];
    self.labelArray = [NSMutableArray arrayWithObjects:@"首页", @"电台", @"阅读", @"社区", @"碎片", @"良品", @"设置", nil];
    self.picArray = [NSMutableArray arrayWithObjects:@"iconfont-tubiao15.png", @"iconfont-yuleyinlediantai-2.png", @"iconfont-yuedu-2.png", @"iconfont-shequ.png", @"iconfont-pengyouwang.png", @"iconfont-shangpinqingdan.png", @"iconfont-shezhi副本.png", nil];
    
    self.newsPicArray = [NSMutableArray arrayWithObjects:@"iconfont-tubiao15-2.png", @"iconfont-yuleyinlediantai-3.png", @"iconfont-yuedu-3.png", @"iconfont-shequ-2.png", @"iconfont-pengyouwang-2.png", @"iconfont-shangpinqingdan-2.png", @"iconfont-shezhi-2.png", nil];

}

-(void)tapAction:(UITapGestureRecognizer *)tap{
    DHMusicPlayerViewController *musicVC = [DHMusicPlayerViewController shareMusicPlayerViewController];
    
    [self.navigationController pushViewController:musicVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (SCREEN_HEIGHT == 736) {
        return 60;
    } else if (SCREEN_HEIGHT == 667) {
        return 50;
    } else if  (SCREEN_HEIGHT == 568) {
        return 45;
    } else if  (SCREEN_HEIGHT == 480) {
        return 35;
    }
    return 44;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    DrawerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
#pragma mark 传值
    [cell setName:[self.labelArray objectAtIndex:indexPath.row] imageName:[self.picArray objectAtIndex:indexPath.row]];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
#pragma mark 页面跳转
    // 首页
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (indexPath.row == 0) {
        TZYHomeViewController *homeVC = [[TZYHomeViewController alloc] init];
        [self.navigationController pushViewController:homeVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:homeVC animated:NO];
    }
    // 电台
    if (indexPath.row == 1) {
        DHRadioViewController *radioVC = [[DHRadioViewController alloc] init];
        [self.navigationController pushViewController:radioVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:radioVC animated:NO];
    }
    // 阅读
    if (indexPath.row == 2) {
        readViewController *readingVC = [[readViewController alloc] init];
        [self.navigationController pushViewController:readingVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:readingVC animated:NO];
    }
    // 社区
    if (indexPath.row == 3) {
        ScocietyViewController *societyVC = [[ScocietyViewController alloc] init];
        [self.navigationController pushViewController:societyVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:societyVC animated:NO];
    }
    // 碎片
    if (indexPath.row == 4) {
        TZYFirstDebrisViewController *debrisVC = [[TZYFirstDebrisViewController alloc] init];
        [self.navigationController pushViewController:debrisVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:debrisVC animated:NO];
    }
    
    // 良品
    if (indexPath.row == 5) {
        TZYGoodsViewController *goodVC = [[TZYGoodsViewController alloc] init];
        [self.navigationController pushViewController:goodVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:goodVC animated:NO];
    }
    
    // 设置
    if (indexPath.row == 6) {
        SetViewController *setVC = [[SetViewController alloc] init];
        [self.navigationController pushViewController:setVC animated:YES];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.naVC pushViewController:setVC animated:NO];
    }

    
    
#pragma mark 设置点击改变图片和label颜色
  
    DrawerTableViewCell *cell = (DrawerTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

#pragma mark 设置点击改变图片和label颜色
    if (self.tempIndex) {
        cell = [tableView cellForRowAtIndexPath:self.tempIndex];
        [cell.label setTextColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1]];
        cell.imageV.image = [UIImage imageNamed:[self.picArray objectAtIndex:self.tempIndex.row]];
        cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.label setTextColor:[UIColor whiteColor]];
        cell.imageV.image = [UIImage imageNamed:[self.newsPicArray objectAtIndex:indexPath.row]];
    } else {
        [cell.label setTextColor:[UIColor whiteColor]];
        cell.imageV.image = [UIImage imageNamed:[self.newsPicArray objectAtIndex:indexPath.row]];
    }
    self.tempIndex = indexPath;
    
}


-(void)pushToPZRSearchViewController
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"推");
    PZRSearchViewController *searchVC = [[PZRSearchViewController alloc]init];
    [tempAppDelegate.naVC presentViewController:searchVC animated:YES completion:^{
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
